<template>
  <div class="app-container">
    <!-- 顶部工具栏 -->
    <div class="toolbar">
      <div class="left-tools">
        <el-button type="primary">保存并新增</el-button>
        <el-button>保存</el-button>
        <el-dropdown>
          <el-button>
            打印
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>打印选项1</el-dropdown-item>
              <el-dropdown-item>打印选项2</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-dropdown>
          <el-button>
            更多
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>选项1</el-dropdown-item>
              <el-dropdown-item>选项2</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="right-tools">
        <el-button-group>
          <el-button>上一页</el-button>
          <el-button>下一页</el-button>
        </el-button-group>
      </div>
    </div>

    <!-- 凭证主体 -->
    <div class="voucher-container">
      <!-- 凭证头部 -->
      <div class="voucher-header">
        <div class="header-item">
          <el-select v-model="voucherForm.word" placeholder="记" style="width: 80px">
            <el-option label="记" value="记" />
            <el-option label="收" value="收" />
            <el-option label="付" value="付" />
            <el-option label="转" value="转" />
          </el-select>
          <el-input v-model="voucherForm.number" style="width: 60px" placeholder="2" />
          <span class="label">号</span>
        </div>
        <div class="header-item">
          <span class="label">日期：</span>
          <el-date-picker
            v-model="voucherForm.voucherDate"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
            style="width: 150px"
          />
        </div>
        <div class="header-item">
          <span class="label">附单据</span>
          <el-input v-model="voucherForm.attachment" style="width: 60px" />
          <span class="label">张</span>
        </div>
      </div>

      <!-- 凭证表格 -->
      <el-table 
        :data="voucherForm.details" 
        border 
        style="width: 100%"
        :cell-style="{ padding: '5px 0' }"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
      >
        <el-table-column label="序号" width="60" align="center">
          <template #default="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="摘要" width="300">
          <template #default="scope">
            <el-input 
              v-model="scope.row.summary" 
              placeholder="请输入摘要"
              :bordered="false"
            />
          </template>
        </el-table-column>
        <el-table-column label="会计科目">
          <template #default="scope">
            <el-select
              v-model="scope.row.subjectId"
              filterable
              placeholder="请选择科目"
              style="width: 100%"
              :bordered="false"
            >
              <el-option
                v-for="item in subjectOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="借方金额" width="200">
          <template #header>
            <div class="amount-header">
              <span v-for="(unit, index) in moneyUnits" :key="index">{{ unit }}</span>
            </div>
          </template>
          <template #default="scope">
            <el-input
              v-model="scope.row.debitAmount"
              placeholder="0.00"
              :bordered="false"
              @change="calculateTotal"
            />
          </template>
        </el-table-column>
        <el-table-column label="贷方金额" width="200">
          <template #header>
            <div class="amount-header">
              <span v-for="(unit, index) in moneyUnits" :key="index">{{ unit }}</span>
            </div>
          </template>
          <template #default="scope">
            <el-input
              v-model="scope.row.creditAmount"
              placeholder="0.00"
              :bordered="false"
              @change="calculateTotal"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 合计行 -->
      <div class="total-row">
        <span>合计：零元整</span>
        <div class="amount-total">
          <span>借方：{{ totalDebit.toFixed(2) }}</span>
          <span>贷方：{{ totalCredit.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 制单人信息 -->
      <div class="voucher-footer">
        <span>制单人：会计002</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

// 金额单位
const moneyUnits = ['亿', '千', '百', '十', '万', '千', '百', '十', '元', '角', '分']

// 表单数据
const voucherForm = reactive({
  word: '记',
  number: '2',
  voucherDate: new Date().toISOString().split('T')[0],
  attachment: 0,
  details: [
    {
      summary: '',
      subjectId: undefined,
      debitAmount: undefined,
      creditAmount: undefined
    }
  ]
})

// 科目选项
const subjectOptions = ref([
  { id: '1001', name: '库存现金' },
  { id: '1002', name: '银行存款' },
  { id: '2001', name: '应付账款' }
])

// 计算借贷方合计
const totalDebit = computed(() => {
  return voucherForm.details.reduce((sum, item) => {
    return sum + (Number(item.debitAmount) || 0)
  }, 0)
})

const totalCredit = computed(() => {
  return voucherForm.details.reduce((sum, item) => {
    return sum + (Number(item.creditAmount) || 0)
  }, 0)
})

// 添加空行
const addEmptyRow = () => {
  voucherForm.details.push({
    summary: '',
    subjectId: undefined,
    debitAmount: undefined,
    creditAmount: undefined
  })
}

// 计算合计
const calculateTotal = () => {
  // 自动添加新行
  if (voucherForm.details[voucherForm.details.length - 1].summary || 
      voucherForm.details[voucherForm.details.length - 1].subjectId ||
      voucherForm.details[voucherForm.details.length - 1].debitAmount ||
      voucherForm.details[voucherForm.details.length - 1].creditAmount) {
    addEmptyRow()
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
  background-color: #fff;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.left-tools {
  display: flex;
  gap: 10px;
}

.voucher-container {
  border: 1px solid #dcdfe6;
  padding: 20px;
}

.voucher-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.label {
  font-size: 14px;
  color: #606266;
}

.amount-header {
  display: flex;
  justify-content: space-around;
}

.total-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #dcdfe6;
  margin: 10px 0;
}

.amount-total {
  display: flex;
  gap: 20px;
}

.voucher-footer {
  margin-top: 20px;
  color: #606266;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  box-shadow: none;
}

:deep(.el-input__inner) {
  text-align: center;
}
</style>